package com.ali.Reggie.controller;

import com.ali.Reggie.common.BaseContext;
import com.ali.Reggie.common.R;
import com.ali.Reggie.entity.ShoppingCart;
import com.ali.Reggie.service.ShoppingCartService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author ：ALi
 * @date ：Created in 2022/6/14 16:40
 * @description：购物车控制类
 * @modified By：
 * @version: $
 */
@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    //添加到购物车
    @PostMapping("/add")
    public R<ShoppingCart> addToCart(@RequestBody ShoppingCart shoppingCart){
        log.info(shoppingCart.toString());
        //获得用户id
        Long currentID = BaseContext.getCurrentID();
        shoppingCart.setUserId(currentID);
        //查询当前菜品或者套餐是否在购物车中
        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,currentID);

        if(dishId != null){
            //添加到购物车的是菜品
            lambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else{
            //添加到购物车的是套餐
            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //从数据库中查出的已经有的记录
        ShoppingCart shoppingCart1 = shoppingCartService.getOne(lambdaQueryWrapper);

        //如果存在数量就加一
        if(shoppingCart1 != null){
            Integer number = shoppingCart1.getNumber();
            shoppingCart1.setNumber(number+1);
            shoppingCartService.updateById(shoppingCart1);
        }else{
            //否则就直接加入购物车
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            shoppingCart1 = shoppingCart;
        }
        return R.success(shoppingCart1);
    }

    //查看购物车内容
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        Long userID = BaseContext.getCurrentID();
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,userID);
        lambdaQueryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(lambdaQueryWrapper);


        return R.success(shoppingCartList);
    }

    //购物车的菜品或者套餐减少一份sub

    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentID());
        if(dishId != null){
            lambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else{
            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart shoppingCartOne = shoppingCartService.getOne(lambdaQueryWrapper);
        Integer number = shoppingCartOne.getNumber();
        if(number > 1){
            shoppingCartOne.setNumber(number-1);
            shoppingCartService.updateById(shoppingCartOne);

        }else{
            shoppingCartService.removeById(shoppingCartOne);
        }
        ShoppingCart res =shoppingCartService.getOne(lambdaQueryWrapper);
        if(res != null){
            return R.success(res);
        }else{
            return R.success(new ShoppingCart());
        }
    }

    //清空购物车
    @DeleteMapping("/clean")
    public R<String> clean(){
        Long userid = BaseContext.getCurrentID();
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,userid);
        shoppingCartService.remove(lambdaQueryWrapper);
        return R.success("删除成功!");
    }



}